﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows;

namespace Common.UI.Controls
{
    public class BooleanToVisibilityConverter : IValueConverter
    {
        BooleanToVisibilityAdapter adapter = new BooleanToVisibilityAdapter();
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            String negated = parameter as String;

            var booleanNullable = value as Boolean?;

            if (!String.IsNullOrEmpty(negated))
            {
                return adapter.GetVisibilityNegated(booleanNullable);
            }
            return adapter.GetVisibility(booleanNullable);           
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    class BooleanToVisibilityAdapter
    {
        public Visibility GetVisibility(bool? value)
        {
            if (value.HasValue && value.Value)
            {
                return Visibility.Visible;
            }
            return Visibility.Collapsed;
        }

        public Visibility GetVisibilityNegated(bool? value)
        {
            if (value.HasValue && value.Value)
            {
                return Visibility.Collapsed;
            }
            return Visibility.Visible;
        }
    }
}

